package ru.amse.tsyganov.jumleditor.model.common;

import java.util.HashSet;
import java.util.Set;

import ru.amse.tsyganov.jumleditor.view.ModelChangeListener;

public class GraphModel extends ModelElement {
	
	private final Set<State> contents = new HashSet<State>();
	
	private final Set<ModelChangeListener> modelChangeListeners = 
		new HashSet<ModelChangeListener>();
	
	public GraphModel() {
	}
	
	public void addState(State state) {
		if (state == null) {
			throw new IllegalArgumentException();
		}
		state.setActivityGraph(this);
		if (contents.add(state)) {
			fireModelChanged();
		}
	}
	
	public boolean removeState(State state) {
		if (state == null) {
			throw new IllegalArgumentException();
		}
		boolean result = contents.remove(state);
		if (result) {
			fireModelChanged();
		}
		return result;
	}
	
	public void removeAllStates() {
		contents.clear();
		fireModelChanged();
	}

	public Iterable<State> getContents() {
		return contents;
	}
	
	public boolean addModelChangeListener(
			ModelChangeListener modelChangeListener) {
		
		return modelChangeListeners.add(modelChangeListener);
	}
	
	public boolean removeModelChangeListener(
			ModelChangeListener modelChangeListener) {
		
		return modelChangeListeners.remove(modelChangeListener);
	}
	
	public Iterable<ModelChangeListener> getModelChangeListeners() {
		return modelChangeListeners;
	}
	
	public void fireModelChanged() {
		for (ModelChangeListener listener : modelChangeListeners) {
			listener.modelChanged(this);
		}
	}
}
